package com.education.user.controller;

import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateLiuUtil;
import com.education.user.pojo.SysInfoMsg;
import com.education.user.pojo.SysUser;
import com.education.user.service.ISysUserService;
import com.education.user.service.SysInfoMsgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 消息 信息操作处理
 *
 * @author d
 * @date 2020-03-24
 */
@RestController
@RequestMapping("/sysInfoMsg")
public class SysInfoMsgController extends BaseController {

    @Autowired
    private SysInfoMsgService sysInfoMsgService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 根据当前登录用户id 查询消息列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(SysInfoMsg sysInfoMsg, Integer pageSize, Integer pageNum) {
        if (pageSize != null && pageNum != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        if (sysInfoMsg.getUserId() != null) {
            List<SysInfoMsg> list = sysInfoMsgService.select(sysInfoMsg.getUserId());
            //格式化创建时间的值
            for (SysInfoMsg infoMsg : list) {
                String createTimeString = infoMsg.getCreateTime().split(" ")[0];
                infoMsg.setCreateTime(createTimeString);
            }
            if (list.size() > 4) {
                //取前八条数据
                List<SysInfoMsg> newList = list.subList(0, 4);
                return new ApiReturnObject("200", "查询成功", newList);
            }
            return new ApiReturnObject("200", "查询成功", list);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }


    /**
     * 根据当前登录用户id 查询消息列表
     */
    @GetMapping("/listAll")
    public ApiReturnObject listAll(Integer userId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SysInfoMsg> list = sysInfoMsgService.selectAll(userId);
        //格式化创建时间的值
        for (SysInfoMsg infoMsg : list) {
            String createTimeString = infoMsg.getCreateTime().split(" ")[0];
            infoMsg.setCreateTime(createTimeString);

            //根据用户id 查询发布消息用户
            SysUser user = sysUserService.selectOne(infoMsg.getPublisher().toString());
            if (user != null) {
                infoMsg.setUserName(user.getSysRelname());
            }
        }
        PageInfo<SysInfoMsg> pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "查询成功", pageInfo);

    }

    /**
     * 根据当前登录用户id 查询消息列表  供远程调用
     */
    @GetMapping("/list2")
    public List<SysInfoMsg> list(Integer userId) {
        List<SysInfoMsg> list = sysInfoMsgService.selectAll(userId);
        //格式化创建时间的值
        for (SysInfoMsg infoMsg : list) {
            String createTimeString = infoMsg.getCreateTime().split("\\.")[0];
            infoMsg.setCreateTime(createTimeString);
        }
        int i = 7;
        if (list.size() > i) {
            //取前八条数据
            List<SysInfoMsg> newList = list.subList(0, 7);
            return newList;
        }
        return list;
    }


    /**
     * 新增保存消息
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SysInfoMsg sysInfoMsg) {
        sysInfoMsgService.insert(sysInfoMsg);
        return new ApiReturnObject("200", "添加成功", "");
    }

    @PostMapping("/addOther")
    public void addSaveOther(SysInfoMsg sysInfoMsg) {
        //已经有 title content type
        String dateTime = DateLiuUtil.getAllFormatterLocalDateTime(LocalDateTime.now());
        //创建时间
        sysInfoMsg.setCreateTime(dateTime);
        //删除标志
        sysInfoMsg.setDelFlag("0");
        sysInfoMsg.setPublisher(sysInfoMsg.getUserId());
        if (sysInfoMsg.getTeaIdList() != null && sysInfoMsg.getTeaIdList().size() > 0) {
            for (Integer integer : sysInfoMsg.getTeaIdList()) {
                sysInfoMsg.setUserId(integer);
                sysInfoMsgService.insert(sysInfoMsg);
            }
        }
    }

}